package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.AllocateOrderDetailRepository;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crunifiedinventory.domain.allocateorder.model.AllocateOrder;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.dto.PageQueryAllocateOrderListByReadonlyRequest;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.dto.PageQueryAllocateOrderListRequest;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.dto.QueryAllocateOrderIdsByNotReturnByReadonlyRequest;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.dto.QueryAllocateOrderIdsByNotReturnRequest;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.dto.StatisticalAllocateOrderGroupByOrgScItemByReadonlyRequest;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.dto.StatisticalAllocateOrderGroupByOrgScItemRequest;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.dto.StatisticalOrderQuantityGroupByCreateByReadonlyRequest;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.dto.StatisticalOrderQuantityGroupByCreateRequest;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.model.AllocateOrderDetail;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.service.AllocateOrderDetailService;
import com.epoch.app.crunifiedinventory.model.dto.GetListOutinForReportRequest;
import com.epoch.app.crunifiedinventory.model.dto.MonthOrderQuantitySDO;
import com.epoch.app.crunifiedinventory.model.dto.WmsOrder;
import com.epoch.app.crunifiedinventory.service.CrUnifiedInventoryService;
import com.epoch.app.otsapplicationreportsdk.contants.OtsApplicationReportSdkConstants;
import com.epoch.app.otsapplicationreportsdk.model.dto.CommPageQueryRequest;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2022/1/14 16:10
 */
@Component
public class AllocateOrderDetailRepositoryImpl implements AllocateOrderDetailRepository {

    @Resource
    AllocateOrderDetailService allocateOrderDetailService;

    @Resource
    CrUnifiedInventoryService crUnifiedInventoryService;

    /**
     * 根据创建日期、收货组织分组，统计非退单的调拨单日订单金额
     * @param request
     * @return
     */
    @Override
    public Integer statisticalOrderQuantityGroupByCreateGetTotal(CommPageQueryRequest request) {
        Result<List<MonthOrderQuantitySDO>> totalResult = null;
        if("1".equals(OtsApplicationReportSdkConstants.QUERY_READONLY_DATABASE())){
            StatisticalOrderQuantityGroupByCreateByReadonlyRequest queryRequest = StatisticalOrderQuantityGroupByCreateByReadonlyRequest.builder()
                    .organizationIds(request.getOrganizationIds())
                    .beginDate(request.getBeginDate())
                    .endDate(request.getEndDate())
                    .queryTotal(true)
                    .build();
            totalResult = allocateOrderDetailService.statisticalOrderQuantityGroupByCreateByReadonly(queryRequest);
        }else{
            StatisticalOrderQuantityGroupByCreateRequest queryRequest = StatisticalOrderQuantityGroupByCreateRequest.builder()
                    .organizationIds(request.getOrganizationIds())
                    .beginDate(request.getBeginDate())
                    .endDate(request.getEndDate())
                    .queryTotal(true)
                    .build();
            totalResult = allocateOrderDetailService.statisticalOrderQuantityGroupByCreate(queryRequest);
        }
        return totalResult.getTotal();
    }

    /**
     * 根据创建日期、收货组织分组，统计非退单的调拨单日订单金额
     * @param request
     * @return
     */
    @Override
    public Result<List<MonthOrderQuantitySDO>> statisticalOrderQuantityGroupByCreate(CommPageQueryRequest request) {
        Result<List<MonthOrderQuantitySDO>> result = null;
        if("1".equals(OtsApplicationReportSdkConstants.QUERY_READONLY_DATABASE())){
            StatisticalOrderQuantityGroupByCreateByReadonlyRequest queryRequest = StatisticalOrderQuantityGroupByCreateByReadonlyRequest.builder()
                    .organizationIds(request.getOrganizationIds())
                    .beginDate(request.getBeginDate())
                    .endDate(request.getEndDate())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .queryTotal(false)
                    .build();
            result = allocateOrderDetailService.statisticalOrderQuantityGroupByCreateByReadonly(queryRequest);
        }else{
            StatisticalOrderQuantityGroupByCreateRequest queryRequest = StatisticalOrderQuantityGroupByCreateRequest.builder()
                    .organizationIds(request.getOrganizationIds())
                    .beginDate(request.getBeginDate())
                    .endDate(request.getEndDate())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .queryTotal(false)
                    .build();
            result = allocateOrderDetailService.statisticalOrderQuantityGroupByCreate(queryRequest);
        }
        return result;
    }

    /**
     * 根据创建日期、收货组织分组，查询非退单的调拨单列表
     * @param request
     * @return
     */
    @Override
    public Result<List<MonthOrderQuantitySDO>> pageQueryAllocateOrderList(CommPageQueryRequest request) {
        Result<List<MonthOrderQuantitySDO>> result = null;
        if("1".equals(OtsApplicationReportSdkConstants.QUERY_READONLY_DATABASE())){
            PageQueryAllocateOrderListByReadonlyRequest queryRequest = PageQueryAllocateOrderListByReadonlyRequest.builder()
                    .organizationIds(request.getOrganizationIds())
                    .beginDate(request.getBeginDate())
                    .endDate(request.getEndDate())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = allocateOrderDetailService.pageQueryAllocateOrderListByReadonly(queryRequest);
        }else{
            PageQueryAllocateOrderListRequest queryRequest = PageQueryAllocateOrderListRequest.builder()
                    .organizationIds(request.getOrganizationIds())
                    .beginDate(request.getBeginDate())
                    .endDate(request.getEndDate())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = allocateOrderDetailService.pageQueryAllocateOrderList(queryRequest);
        }
        return result;
    }

    /**
     *  根据分子公司、货品分组，统计非退单的调拨单日订单量
     * @param request
     * @return
     */
    @Override
    public Result<List<AllocateOrderDetail>> statisticalAllocateOrderGroupByOrgScItem(CommPageQueryRequest request){
        Result<List<AllocateOrderDetail>> result = null;
        if("1".equals(OtsApplicationReportSdkConstants.QUERY_READONLY_DATABASE())){
            StatisticalAllocateOrderGroupByOrgScItemByReadonlyRequest queryRequest = StatisticalAllocateOrderGroupByOrgScItemByReadonlyRequest.builder()
                    .orgBusinessUnitIds(request.getOrganizationIds())
                    .beginDate(request.getBeginDate())
                    .endDate(request.getEndDate())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = allocateOrderDetailService.statisticalAllocateOrderGroupByOrgScItemByReadonly(queryRequest);
        }else{
            StatisticalAllocateOrderGroupByOrgScItemRequest queryRequest = StatisticalAllocateOrderGroupByOrgScItemRequest.builder()
                    .orgBusinessUnitIds(request.getOrganizationIds())
                    .beginDate(request.getBeginDate())
                    .endDate(request.getEndDate())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = allocateOrderDetailService.statisticalAllocateOrderGroupByOrgScItem(queryRequest);
        }
        return result;
    }

    /**
     * 查询非退单的调拨单id
     * @param request
     * @return
     */
    @Override
    public Result<List<AllocateOrder>> queryAllocateOrderIdsByNotReturn(CommPageQueryRequest request) {
        Result<List<AllocateOrder>> result = null;
        if("1".equals(OtsApplicationReportSdkConstants.QUERY_READONLY_DATABASE())){
            QueryAllocateOrderIdsByNotReturnByReadonlyRequest queryRequest = QueryAllocateOrderIdsByNotReturnByReadonlyRequest.builder()
                    .orgBusinessUnitIds(request.getOrganizationIds())
                    .beginDate(request.getBeginDate())
                    .endDate(request.getEndDate())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = allocateOrderDetailService.queryAllocateOrderIdsByNotReturnByReadonly(queryRequest);
        }else{
            QueryAllocateOrderIdsByNotReturnRequest queryRequest = QueryAllocateOrderIdsByNotReturnRequest.builder()
                    .orgBusinessUnitIds(request.getOrganizationIds())
                    .beginDate(request.getBeginDate())
                    .endDate(request.getEndDate())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = allocateOrderDetailService.queryAllocateOrderIdsByNotReturn(queryRequest);
        }
        return result;
    }

    /**
     * 根据SAP调拨单号批量查询入库单
     * @param outAllocateOrderIds
     * @return
     */
    @Override
    public Result<List<WmsOrder>> queryOutinOrdersByOutAllocateOrderIds(List<String> outAllocateOrderIds,Integer start,Integer limit) {
        GetListOutinForReportRequest queryRequest = GetListOutinForReportRequest.builder()
                .outAllocateOrderList(outAllocateOrderIds)
                .orderStatus("2")//完成
                .subMainOrderType("1")//入库单
                .isAllocate(1)//调拨单
                .start(start)
                .size(limit)
                .build();
        Result<List<WmsOrder>> result = crUnifiedInventoryService.getListOutinForReport(queryRequest);
        return result;
    }

//    @Override
//    public List<MonthOrderQuantitySDO> statisticalAccumulativeOrderQuantityByOrg(CommPageQueryRequest request) {
//        StatisticalAccumulativeOrderQuantityByOrgRequest queryRequest = StatisticalAccumulativeOrderQuantityByOrgRequest.builder()
//                .organizationIds(request.getOrganizationIds())
//                .beginDate(request.getBeginDate())
//                .endDate(request.getEndDate())
//                .build();
//        Result<List<MonthOrderQuantitySDO>> result = allocateOrderDetailService.statisticalAccumulativeOrderQuantityByOrg(queryRequest);
//        return result.getResult();
//    }

}
